Here we are going to create a sample of displaying notification on our click. In this sample we will use UILocalNotification and UISwitch classes that are provided in UIKit framework, before creating this sample we will discuss about UILocalNotification and UISwitch:
By configuring the properties of UILocalNotification, you can use the UIApplication class to trigger the notification to arrive on screen either immediately or at a specified time and date. UIApplication handles putting the notification on the screen when required. Since the release of iOS 4, Apple introduced a new type of notification called Local Notification. This makes pushing a notification much simpler. No more server side programming. No more Internet connection is required. You can schedule a notification with simple API and it’ll be fired up at a proper time.
To demonstrate the usage of UILocalNotification we’ll build a simple, on this sample we will provide a list of colors and a switch on each cell, when the user switch on any of the available color on the list, a notification reminds users about the selection of color. The UISwitch control is used to display the state of a Boolean object in iOS8. You use the UISwitch class to create and manage the On/Off buttons.
Creating a sample using UILocalNotification class
• create a new project using the “Single View application” temple.
• create the user interface and add the table view.
Select “SimpleTableViewController.xib” to switch to the Interface Builder.
• In the Object Library, select the “Table View”
object and drag it into the view.
• Your screen should look like below after
inserting the “Table View”.
Define a local notification:
code of ViewController.m file
@implementation ViewController
@synthesize switchObject,switchLabel,arrayList;
// Called after the controller’s view is loaded into memory.
- (void)viewDidLoad{
[super viewDidLoad];
// defining list of color into the arrayList
arrayList = [NSMutableArray arrayWithObjects:
@"Red",@"Green",@"Blue",@"Yellow",@"Orange",
@"White",@"Pink",@"Black", nil];
}
// this method return the no of rows in the arrayList
-(NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section{
return arrayList.count;
}
// this method insert the value in each cell of UITableView
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *simpleTableIdentifier = @"TableCell";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:
UITableViewCellStyleDefault reuseIdentifier:
simpleTableIdentifier];
switchObject = [[UISwitch alloc] init];
switchObject.tag = indexPath.row;
[switchObject addTarget:self action:@selector(switchChanged:
withobject:) forControlEvents:
UIControlEventValueChanged];
[switchObject setOn:YES];
cell.accessoryView = switchObject;
}
cell.textLabel.text = [arrayList objectAtIndex:indexPath.row];
return cell;
}
// this method is colled when switch is being ON or OFF on every action
-(void)switchChanged:(UISwitch *)switcher withobject:
(NSIndexPath*)index{
// here we defining and initializing UILocalNotification
UILocalNotification *localNotification =
[[UILocalNotification alloc] init];
// here we defining the time when the notificaition will be displayed
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:0];
// here we defining the Body that is Displayed on swithching the switch
localNotification.alertBody =[arrayList objectAtIndex:switcher.tag];
// this method is used to call the localNotification
[[UIApplication sharedApplication] scheduleLocalNotification:
localNotification];
}
@end
This basically setup a local notification to fire in seconds in the future from when the app view comes on screen.
Code of ViewController.h file
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong) IBOutlet UISwitch *switchObject;
@property IBOutlet UILabel *switchLabel;
@property NSMutableArray *arrayList;
@end
code of AppDelegate.m file
- this delegate method is used to register the notification method. add it into AppDelegate.m file.
// here we registering our local notification
- (void)application:(UIApplication *)application didReceiveLocalNotification:
(UILocalNotification *)notification {
[self showAlarm:notification.alertBody];
}
Display the alert
code in AppDelegate.m file
// customized method
- (void)showAlarm:(NSString *)text {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:
@"Notification"
message:text
delegate:nil
cancelButtonTitle:nil
otherButtonTitles:nil];
[alertView show];
[self performSelector:@selector(test:) withObject:
alertView afterDelay:2];
}
// this method is used to dismiss the notification after some time
-(void)test:(UIAlertView *)x{
[x dismissWithClickedButtonIndex:-1 animated:YES];
}
Now, run the application and you will see the switches are added in each cell of Table View. Okay, let’s compile and run the app. :
now, when you switch the button a notification will be displayed for some time and after it will be disappear.
[ Notification is device depended it can appear every device differently ]
Leave Comment